package com.machine.filling.function.set.excOrderDetail;

import android.content.Context;

import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.modle.ClientInfoModle;
import com.machine.filling.db.modle.ExceptionOrderModle;
import com.machine.filling.db.modle.TempOrderModle;
import com.machine.filling.function.set.orderDetail.OrderDetailContract;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.util.Common;
import com.machine.filling.util.CustomToast;
import com.machine.filling.util.DateUtils;
import com.machine.filling.util.HexUtils;
import com.machine.filling.util.SpUtils;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/4/11 0011.
 */

public class ExcOrderDetailPresenter implements ExcOrderDetailContract.Presenter {
    private Context mContext;
    private ExcOrderDetailContract.View mView;
    private ExceptionOrderModle orderModle;

    public ExcOrderDetailPresenter(Context mContext, ExcOrderDetailContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        start();
    }

    @Override
    public void start() {
        orderModle = mView.getOrder();
    }

    @Override
    public void distoryContext() {

    }

    /**
     * 油卡加油 创建订单
     */
    @Override
    public void createCardFuelingOrder() {
        mView.showProgress("订单上传中...");
        final ExceptionOrderModle modle = mView.getOrder();
        ClientInfoModle client = ClientInfoDao.getInstance().getModleByDeviceNum();
        APIHttpManager.getInstance().createNewOrder(
                client.getToken(),
                modle.getAmount(),
                modle.getMachineNum(),
                modle.getDiscountPrice(),
                modle.getOrderNum(),
                modle.getOilPotId(),
                modle.getOilGunNum(),
                modle.getOilGunId(),
                modle.getSellerId(),
                modle.getMachineId(),
                modle.getOilCardId(),
                modle.getPrice(),
                HexUtils.formatDouble(Double.parseDouble(orderModle.getMlAmount()) * 1000, 2),
                modle.getOilTypeName(),
                modle.getOilTypeId(),
                modle.getCuPrice(),
                modle.getOilPotNum(),
                Common.COMMIT_ORDER_EXCEPTION,
                Integer.parseInt(modle.getMachineFlowNum(), 16) + "",
                DateUtils.formatDateTimeYMDHMS(modle.getCreateD()),
                new BaseHttpStringCallback() {
                    @Override
                    public void onSuccess(Call call, String result) {
                        ExceptionOrderDao.getInstance().updateOrderUploadStateByOrderNum(modle.getOrderNum(),true,"");

                        //  判断是否未支付, 未支付,调用支付接口
                        if ("3".equals(modle.getPayState())){
                            //已支付
                            mView.hideProgress();
                        }else {
                            //未支付,去调用现金支付接口
                            //员工卡 -->现金支付, 用户卡-->油卡支付   1用户卡,2员工卡
                            if ("1".equals(modle.getOilCardType())){
                                payByOilCard();
                            }else {
                                payByCash();
                            }
                            //订单更新订单状态
                            mView.showCreateCardFuelingOrderSuccess();
                        }
                    }

                    @Override
                    public void onFailed(String errorCode, String errorMsg, Exception exception) {
                        ExceptionOrderDao.getInstance().updateOrderUploadStateByOrderNum(modle.getOrderNum(),false,errorMsg);
                        mView.hideProgress();
                        mView.showCreateCardFuelingOrderFailed();
                    }

                    @Override
                    public void onFinished() {

                    }
                }
        );
    }

    /**
     * 现金 支付
     */
    @Override
    public void payByCash() {
        final ExceptionOrderModle modle = mView.getOrder();
        APIHttpManager.getInstance().payByCash(modle.getToken(), modle.getMachineId(),
                modle.getOrderNum(), new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                ExceptionOrderDao.getInstance().updateOrderPayStateByOrderNum(modle.getOrderNum(), Common.ORDER_PAYED,"");
                ExceptionOrderDao.getInstance().upadteOrderPayModeByOrderNum(modle.getOrderNum(),Common.PAY_MODE_CASH);
                mView.hideProgress();
                mView.showPayByCashSuccess();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                ExceptionOrderDao.getInstance().updateOrderPayStateByOrderNum(modle.getOrderNum(), Common.ORDER_UNPAY,errorMsg);
                mView.hideProgress();
                mView.showPayByCashFailed();
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 油卡 支付
     */
    @Override
    public void payByOilCard() {
        final ExceptionOrderModle modle = mView.getOrder();

        APIHttpManager.getInstance().payByCard(modle.getToken(), modle.getMachineId(), modle.getOrderNum(), new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                ExceptionOrderDao.getInstance().updateOrderPayStateByOrderNum(modle.getOrderNum(),Common.ORDER_PAYED,"");
                ExceptionOrderDao.getInstance().upadteOrderPayModeByOrderNum(modle.getOrderNum(),Common.PAY_MODE_CARD);
                mView.showPayByCardSuccess();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                ExceptionOrderDao.getInstance().updateOrderPayStateByOrderNum(modle.getOrderNum(),Common.ORDER_UNPAY,errorMsg);
                mView.showPayByCardFailed();

            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 提交预约订单
     */
    @Override
    public void createPreOrder() {
        mView.showProgress("订单上传中...");
        final ExceptionOrderModle modle = mView.getOrder();
        APIHttpManager.getInstance().caretePreOrder(
                modle.getToken(),
                modle.getOrderNum(),
                modle.getOilGunId(),
                modle.getMachineId(),
                modle.getOilPotId(),
                modle.getOilGunNum(),
                modle.getOilTypeId(),
                modle.getUsePrice(),
                modle.getPrice(),
                HexUtils.formatDouble(Double.parseDouble(orderModle.getMlAmount()) * 1000, 2),
                modle.getMachineNum(),
                modle.getOilTypeName(),
                modle.getCuPrice(),
                modle.getOilPotNum(),
                Integer.parseInt(modle.getMachineFlowNum(), 16) + "",
                Common.COMMIT_ORDER_EXCEPTION,
                DateUtils.formatDateTimeYMDHMS(modle.getCreateD()),
                new BaseHttpStringCallback() {
                    @Override
                    public void onSuccess(Call call, String result) {
                        //todo 订单更新订单状态
                        ExceptionOrderDao.getInstance().updateOrderUploadStateByOrderNum(modle.getOrderNum(),true,"");
                        mView.showCreatePreOrderSuccess();

                    }

                    @Override
                    public void onFailed(String errorCode, String errorMsg, Exception exception) {
                        ExceptionOrderDao.getInstance().updateOrderUploadStateByOrderNum(modle.getOrderNum(),false,errorMsg);
                        mView.showCreatePreOrderFailed();
                    }

                    @Override
                    public void onFinished() {
                        mView.hideProgress();
                    }
                }

        );
    }

    @Override
    public void uploadExceptionOrder() {
        // TODO: 2018/5/24 0024 ,先判断加油方式:
        // TODO: 1.油卡加油(a.客存:b.员工卡)[如果未创建订单,则先创建订单,后支付(客存为油卡支付,员工卡为现金支付)]--[如果已创建订单,直接调支付接口]
        // TODO: 2.预约加油 (未创建订单,应创建订单)
        String orderType = orderModle.getOrderType();//是否预约  1预约 0未预约
        if ("1".equals(orderType)) {
            //预约加油
            createPreOrder();
        } else {
            //是否上传订单 0 未上传 1已上传
            if ("0".equals(orderModle.getUploadState())) {
                //未上传, 先上传订单,然后,进行支付(用户卡:油卡支付;  员工卡:现金支付)
                createCardFuelingOrder();
            } else {
                //已上传,检查是否支付,未支付的(用户卡:油卡支付;  员工卡:现金支付)
                // 加油类型: 1用户的加油卡 2员工卡
                if ("1".equalsIgnoreCase(orderModle.getOilCardType())) {
                    payByOilCard();
                } else {
                    payByCash();
                }
            }
        }
    }

}
